package com.nph.fireworks.client.mvp;

import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.nph.fireworks.client.events.StartEvent;

public class ConfigPresenter implements com.nph.fireworks.client.mvp.Presenter {

	private final EventBus eventBus;
	private final ConfigView view;

	public interface Display {
		HasValue<Date> getSelectedDate();
		HasClickHandlers getStartButton();
	}
	
	public ConfigPresenter(EventBus eventBus, ConfigView view) {
		this.eventBus = eventBus;
		this.view = view;
		
		bind();
	}

	private void bind() {
		
		view.getStartButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
//				Window.alert("in onClick");
				
				Date d = view.getSelectedDate().getValue();
				Date now = new Date();
				
				if (d.getTime() < now.getTime()) {
					Window.alert("Please pick a date after the current date.");
					return;
				}
				
				History.newItem("start");
				
				StartEvent startEvent = new StartEvent(d);
				eventBus.fireEvent(startEvent);
			}});
		
	}

	public void go(final HasWidgets container) {
		container.clear();
		container.add(view.asWidget());
	}

}
